import type { AsyncDataResult, Data } from '../define/types'
import type ICrud from '../define/crud'
import transaction from './transaction'
import _ from 'lodash'

class Crud implements ICrud {

  async insert(table: string, data: Data): AsyncDataResult {
    console.log(table, data)
    return await transaction.runInTransaction(async (trx) => {
      return await trx(table).insert(data)
    })
  }

  async update(table: string, where: any, data: any): AsyncDataResult {
    return await transaction.runInTransaction(async (trx) => {
      return await trx(table).where(where).update(data)
    })
  }

  async drop(table: string, where: any): AsyncDataResult<string> {
    return await transaction.runInTransaction(async (trx) => {
      return await trx(table).where(where).del()
    })
  }
}

export default new Crud()
